package com.xzy.learn.spring.lookup;

import com.xzy.learn.spring.entity.User;
import com.xzy.learn.spring.ioc.overview.dependency.lookup.DependencyLookupDemo;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

/**
 * 使用 {@link org.springframework.beans.factory.ObjectProvider} 实现延迟查找
 * （更多的依赖查找方式见父类）
 *
 * @author xzy.xiao
 * @date 2022/6/6  14:31
 */
public class ObjectProviderDemo extends DependencyLookupDemo {
    @Bean
    public User user() {
        return new User(1001L, "lazy");
    }

    public static void main(String[] args) {
        lookupLazyByObjectProvider();
    }

    /**
     * 使用 {@link org.springframework.beans.factory.ObjectProvider} 实现延迟查找
     */
    public static void lookupLazyByObjectProvider() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(ObjectProviderDemo.class); // 注意：
        applicationContext.refresh();

        ObjectProvider<User> beanProvider = applicationContext.getBeanProvider(User.class);
        System.out.println(beanProvider.getObject());
    }
}
